package cn.demon.order.service.impl;

import cn.demon.basic.query.BaseQuery;
import cn.demon.basic.util.LoginContext;
import cn.demon.basic.util.PageList;
import cn.demon.order.domain.OrderPetAcquisition;
import cn.demon.order.dto.OrderDto;
import cn.demon.order.mapper.OrderPetAcquisitionMapper;
import cn.demon.order.query.OrderPetAcquisitionQuery;
import cn.demon.order.service.IOrderPetAcquisitionService;
import cn.demon.basic.service.impl.BaseServiceImpl;
import cn.demon.org.domain.Employee;
import cn.demon.org.domain.Shop;
import cn.demon.org.mapper.EmployeeMapper;
import cn.demon.org.mapper.ShopMapper;
import cn.demon.pet.domain.SearchMasterMsg;
import cn.demon.pet.mapper.PetMapper;
import cn.demon.pet.mapper.SearchMasterMsgMapper;
import cn.demon.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {


    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private PetMapper petMapper;

    @Override
    public PageList<OrderPetAcquisition> queryPage(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLoginUser(request);
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        if(employee.getShop_id()!=null){//店铺人员
            Shop shop = shopMapper.loadById(employee.getShop_id());
            //如果当前登录人的id  不等于  店铺的管理员id
            if(shop.getAdmin_id() != employee.getId()){//店员
                query.setEmpId(employee.getId());
            }
            query.setShopId(shop.getId());
        }

        return super.queryPage(query);
    }


    @Override
    public void confirm(OrderDto orderDto) {
        Long orderId = orderDto.getId();
        Integer payType = orderDto.getPayType();
        BigDecimal money = orderDto.getMoney();

        //1.修改订单：state = 1，money，payType
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(orderId);
        order.setState(1);
        order.setPrice(money);
        order.setPaytype(payType);
        orderPetAcquisitionMapper.update(order);

        //2.修改寻主消息：state=2，note
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(order.getSearchMasterMsgId());
        msg.setState(2);
        msg.setNote("处理完成，交易成功");
        searchMasterMsgMapper.update(msg);

        //宠物
        //修改一个成本价 = money

        //@TODO - 支付模块
    }

    @Override
    public void del(Long id) {
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(id);
        //将寻主消息中的shop_id置空-  寻主池
        searchMasterMsgMapper.reject(order.getSearchMasterMsgId());
        //删除宠物 - 没有成功收购
        petMapper.remove(order.getPetId());
        //删除订单
        super.del(id);
    }
}
